package com.nosweattechnet.server;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import com.nosweattechnet.datatypes.Place;

public class PlaceListAsyncTask extends CommunicatorAsyncTask {
	private BasicNameValuePair tag=new BasicNameValuePair("tag","getPlaceList");
	private String userIDString="user_uuid";
	
	//Creates a new Async task and adds the associated values to the name value pairs
	public PlaceListAsyncTask(String userid) {
		super();
		super.addNameValuePair(tag);
		super.addNameValuePair(new BasicNameValuePair(userIDString,userid));
	}
	
	//Returns the list of Places in the PlaceList by parsing the JSONArray returned from the
	//database query
	public ArrayList<Place> getPlaces() throws InterruptedException, ExecutionException{
		ArrayList<Place> ai=new ArrayList<Place>();
		JSONArray jarr=this.get();
		if(jarr==null){
			return ai;
		}
		for(int i=0;i<jarr.length();i++){
			try {
				ai.add(new Place(jarr.getJSONObject(i)));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return ai;
	}
}
